import { defineStore } from 'pinia'
import { requestLoginByUserId, requestUserLoginOut } from '../api/index'
import { IUserInfoModel } from '../model/index'
import { requestVerificationCodeByEmail, requestVerificationCodeExpire, requestResetPassword } from '@/api'

const userInfoHistory: any = JSON.parse(sessionStorage.getItem('userInfo') || '{}') 
const tokenHistory: string = sessionStorage.getItem('token') || ''

export default defineStore('overall', {
    state: () => {
        return {
            userInfo: userInfoHistory,
            token: tokenHistory,
            selectPage: ''
        }
    },
    getters: {
        getUserInfo(): IUserInfoModel {
            return this.userInfo
        },
        getToken(): string {
            return this.token
        },
        getSelectPage(): string {
            if(sessionStorage.getItem('selectPage')) {
                this.selectPage = sessionStorage.getItem('selectPage') + ''
            }
            return this.selectPage
        }
    },
    actions: {
        // 获取邮箱验证码
        getVerificationCodeByEmail(data: Object) {
            return new Promise((resolve, reject) => {
                requestVerificationCodeByEmail(data).then(response => {
                    const res: any = response.data
                    if (res.code === 200) {
                        resolve(res.data)
                    }
                })
            })
        },

        // 判断验证码是否过期
        getVerificationCodeExpire(data: Object) {
            return new Promise((resolve, reject) => {
                requestVerificationCodeExpire(data).then(response => {
                    const res: any = response.data
                    if (res.code === 200) {
                        resolve(res.data)
                    }
                })
            })
        },

        // 使用账号登录
        getLoginByUserId(data: Object) {
            return new Promise((resolve, reject) => {
                requestLoginByUserId(data).then((response: any) => {
                    const res: any = response.data
                    if (res.code === 200) {
                        if(res.data.status === 'success') {
                            this.userInfo = res.data.results
                            this.token = res.data.token
                            window.sessionStorage.setItem('userInfo', JSON.stringify(res.data.results))
                            window.sessionStorage.setItem('token', res.data.token)
                        }
                        resolve(res.data)
                    }
                })
            })
        },

        // 登出系统
        // getLoginOut() {
        //     return new Promise((resolve, reject) => {
        //         requestUserLoginOut().then((response: any) => {
        //             console.log('response', response)
        //             sessionStorage.removeItem('userInfo')
        //             sessionStorage.removeItem('token')
        //             resolve('success')
        //         })
        //     })
        // },

        // 重置密码
        getResetPassword(data: any) {
            return new Promise((resolve, reject) => {
                requestResetPassword(data).then((response: any) => {
                    const res: any = response.data
                    if (res.code === 200) {
                        resolve(res.data)
                    }
                })
            })
        },

        setUserInfo(userInfo: IUserInfoModel, token: any) {
            this.userInfo = userInfo
            this.token = token
            window.sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
            window.sessionStorage.setItem('token', token)
        },

        setSelectPage(val: string) {
            this.selectPage = val
            sessionStorage.setItem('selectPage', val)
        }
    }
})
